﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CL2036.UI
{
    /// <summary>
    /// UI_Config.xaml 的交互逻辑
    /// </summary>
    public partial class UI_Config : UserControl
    {
        private ComboBox[] m_485Com = new ComboBox[0];

        private Model.SystemConfig m_Config;

        public UI_Config()
        {
            InitializeComponent();
        }

        public UI_Config(Model.SystemConfig vConfig)
            : this()
        {

            m_Config = vConfig;

            Cmb_Bws.Text = m_Config.MeterBwCount.ToString();

            Cmb_485Count.Text = m_Config.ChannelCount.ToString();

            Txt_OpenThreadCount.Text = m_Config.ThreadCount.ToString();

            Txt_2036.Text = m_Config.CL2036Channel.ToString();

            Txt_2018.Text = m_Config.CL20181IP;

            Chk_Dx.IsChecked = m_Config.IsDx;

            Chk_188H.IsChecked = m_Config.Is188H;
        }



        private void Cmb_Bws_TextChanged(object sender, TextChangedEventArgs e)
        {
            Cmb_485Count.Items.Clear();
            try
            {
                for (int i = 0; i < int.Parse(Cmb_Bws.Text); i++)
                {
                    Cmb_485Count.Items.Add(string.Format("{0}", i + 1));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            try
            {
                Cmb_485Count.SelectedItem = m_Config.ChannelCount.ToString();
            }
            catch
            {
                Cmb_485Count.SelectedItem = Cmb_Bws.Text;
            }
        }

        private void Cmb_485Count_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (m_Config.ThreadCount == 0)
                {
                    Txt_OpenThreadCount.Text = Cmb_485Count.SelectedItem.ToString();
                }
                else
                {
                    Txt_OpenThreadCount.Text = m_Config.ThreadCount.ToString();
                }
                Create485Combo(int.Parse(Cmb_485Count.SelectedItem.ToString()));
            }
            catch { Txt_OpenThreadCount.Text = "1"; }
        }

        private void Create485Combo(int Count)
        {
            int cmbCount = 0;

            foreach (System.Windows.UIElement ui in Grid_Ole.Children)
            {
                if (ui is ComboBox)
                {
                    cmbCount++;
                }
            }

            string[] TmpArr = new string[64];

            for (int i = 0; i < 64; i++)
            {
                TmpArr[i] = string.Format("通道{0:D2}", i + 1);
            }

            if (cmbCount == Count) return;

            Grid_Ole.Children.Clear();

            m_485Com = new ComboBox[Count];

            for (int i = 0; i < m_485Com.Length; i++)
            {
                m_485Com[i] = new ComboBox();

                m_485Com[i].ItemsSource = TmpArr;

                m_485Com[i].SelectedIndex = m_Config.RouteChannel(i + 1) - 1;

                m_485Com[i].Margin = new Thickness(0, 2, 2, 2);

                m_485Com[i].Width = 80;

                m_485Com[i].Height = 20;

                TextBlock txt = new TextBlock();

                txt.Text = string.Format("线路{0:d2}:", i + 1);

                txt.Margin = new Thickness(4, 5, 4, 2);

                StackPanel sp = new StackPanel();

                sp.Orientation = Orientation.Horizontal;

                sp.VerticalAlignment = System.Windows.VerticalAlignment.Top;

                sp.Children.Add(txt);

                sp.Children.Add(m_485Com[i]);

                Grid_Ole.Children.Add(sp);
            }

        }


        internal bool Save()
        {
            if (!this.IsLoaded) return true;
            int tmp;
            if (!int.TryParse(Cmb_Bws.Text, out tmp))
            {
                MessageBox.Show("表位数必须为一个整数...", "错误", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                return false;
            }

            if (!int.TryParse(Txt_OpenThreadCount.Text, out tmp))
            {
                MessageBox.Show("开启线程数必须为一个整数...", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }
            if (Cmb_485Count.SelectedIndex == -1)
            {
                MessageBox.Show("485通道数必须进行选择...", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }

            if (!int.TryParse(Txt_2036.Text, out tmp))
            {
                MessageBox.Show("CL2036的通信通道必须是一个数字...", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }

            if (Txt_2018.Text.Split('.').Length != 4)
            {
                MessageBox.Show("必须填写正确的IP地址...", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }

            m_Config.MeterBwCount = int.Parse(Cmb_Bws.Text);

            m_Config.ChannelCount = int.Parse(Cmb_485Count.Text);

            m_Config.ThreadCount = int.Parse(Txt_OpenThreadCount.Text);

            m_Config.CL2036Channel = int.Parse(Txt_2036.Text);

            m_Config.Is188H = (bool)Chk_188H.IsChecked;

            m_Config.IsDx = (bool)Chk_Dx.IsChecked;

            m_Config.CL20181IP = Txt_2018.Text;

            for (int i = 0; i < m_485Com.Length; i++)
            {
                m_Config.SetRouteChannel(i + 1, m_485Com[i].SelectedIndex + 1);
            }
            m_Config.Save();
            return true;
        }
    }
}
